from django.shortcuts import render
from myapp.models import *
from rest_framework.views import APIView
from rest_framework.response import Response
from myapp.ser import *


class BrandView(APIView):
    def get(self, request):
        try:
            brand = BrandModel.objects.all()
        except Exception as e:
            print(e)
            return Response({
                'code': 400,
                'msg': '获取品牌失败'
            })
        brand = BrandSer(brand, many=True)
        return Response({
            'code': 200,
            'msg': '获取品牌成功',
            'brand': brand.data
        })

    def post(self, request):
        name = request.data.get('name')
        try:
            BrandModel.objects.create(name=name)
        except Exception as e:
            print(e)
            return Response({
                'code': '400',
                'msg':'添加失败'
            })
        return Response({
            'code': '200',
            'msg': '添加成功'
        })

    def put(self, request):
        id = request.data.get('brandid')
        name = request.data.get('name')
        try:
            brand = BrandModel.objects.get(id=id)
            brand.name = name
            brand.save()
        except Exception as e:
            print(e)
            return Response({
                'code': '400',
                'msg':'修改失败'
            })
        return Response({
            'code': '200',
            'msg': '修改成功'
        })

    def delete(self, request):
        id = request.data.get('id')
        try:
            BrandModel.objects.get(id=id).delete()
        except Exception as e:
            print(e)
            return Response({
                'code': '400',
                'msg':'删除失败'
            })
        return Response({
            'code': '200',
            'msg': '删除成功'
        })


class CarView(APIView):
    def get(self, request):
        try:
            brand = CarModel.objects.all()
        except Exception as e:
            print(e)
            return Response({
                'code': 400,
                'msg': '获取品牌失败'
            })
        brand = CarSer(brand, many=True)
        return Response({
            'code': 200,
            'msg': '获取汽车成功',
            'car': brand.data
        })


class CateView(APIView):
    def get(self, request):
        try:
            brand = CateModel.objects.all()
        except Exception as e:
            print(e)
            return Response({
                'code': 400,
                'msg': '获取品牌失败'
            })
        brand = CateSer(brand, many=True)
        return Response({
            'code': 200,
            'msg': '获取分类成功',
            'cate': brand.data
        })


class BookView(APIView):
    def get(self, request):
        try:
            brand = BookModel.objects.all()
        except Exception as e:
            print(e)
            return Response({
                'code': 400,
                'msg': '获取品牌失败'
            })
        brand = BookSer(brand, many=True)
        return Response({
            'code': 200,
            'msg': '获取书籍成功',
            'book': brand.data
        })




